#ifndef _THREAD_HPP_
#define _THREAD_HPP_

#include <iostream>
#include <string>
#include <pthread.h>
#include <unistd.h>
#include <cstdlib>
#include <cstdio>
#include <cstdint>
#include <cstring>
#include <functional>


namespace ThreadMod
{
    static uint32_t number = 1;
    class Thread
    {
        private:
            using func_t = std::function<void()>;
            //设置为静态成员函数是因为，作为成员函数是，会有一个this指针的参数，
            //在传一个void*的指针，参数类型和数量都不匹配
            static void * routine(void* args)
            {
                Thread* self = static_cast<Thread* >(args);
                self->setRunning();
                if(self->_isdetach)
                    self->setDetach();
                pthread_setname_np(self->_tid,self->_name.c_str());
                self->_func();//回调函数
                return nullptr;
            }

            void setRunning()
            {
                _isrunning = true;
            }

            void setDetach()
            {
                std::cout << "线程被分离" << std::endl;
                _isdetach = true;
            }
        public:
            Thread(func_t func)
                    :_tid(0),
                     _isrunning(false),
                     _isdetach(false),
                     _res(nullptr),
                     _func(func)
            {
                _name = "新线程" + std::to_string(number++);
            }

            void Detach()
            {
                if(_isdetach)
                    return;
                if(_isrunning)
                    pthread_detach(_tid);
                setDetach();
            }

            bool start()
            {
                if(_isrunning)
                    return false;
                int n = pthread_create(&_tid, nullptr, routine, this);
                if(n > 0)
                {
                    std::cout << "创建失败" << std::endl;
                    return false;
                }
                else 
                {
                    _isrunning = true;
                    std::cout << "创建成功" << std::endl;
                    return true;
                }
            }

            bool stop()
            {
                if(_isrunning)
                {
                    int n = pthread_cancel(_tid);
                    if(n != 0)
                    {
                        std::cout << "创建线程失败" <<std::endl;
                        return false;
                    }
                    else 
                    {
                        _isrunning = true;
                        std::cout << _name << "stop" << std::endl;
                        return true;
                    }
                        
                }
                return false;
            }

            bool join()
            {
                if(_isdetach)
                {
                    std::cout << "线程已经分离，无需等待" << std::endl;
                    return false;
                }
                if(_isrunning)
                {
                    std::cout << "线程正在运行，无法等待" << std::endl;
                    return false;
                }

                if(!_isdetach && !_isrunning)
                {
                    int n = pthread_join(_tid, &_res);
                    if(n > 0)
                    {
                        std::cout << "等待失败" << std::endl;
                    }
                }
                return true;
            }

            ~Thread()
            {}
        private:
            std::string _name;
            pthread_t _tid;
            bool _isrunning;
            bool _isdetach;
            void* _res;
            func_t _func;
    };
}

#endif